﻿using DJ.Dialog;
using NSJL.Biz.Background.Client;
using NSJL.DAL.DataAccess;
using NSJL.DAL.DataModel.Entities;
using NSJL.DomainModel.Background.System;
using NSJL.Framework.Utils;
using NSJL.Plugin.Third.ThirdParty;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DJ
{
    /// <summary>
    /// Start.xaml 的交互逻辑
    /// </summary>
    public partial class Start : Window
    {
        CommBiz biz = new CommBiz();
        public Start()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            string cpu = SelfUtil.GetCPUSerialNumber();
            List<AuthenticationInfo> authenticationInfos = biz.GetAuthInfo(cpu);
            if (authenticationInfos.Count <= 0)
            {
                ActivationView activationView = new ActivationView();
                this.Visibility = Visibility.Hidden;
                activationView.ShowDialog();
            }
            if (authenticationInfos.Count > 0 && authenticationInfos[0].e < DateTime.Now)
            {
                AuthExpiresView authExpiresView = new AuthExpiresView();
                this.Visibility = Visibility.Hidden;
                authExpiresView.ShowDialog();
            }
            authenticationInfos = biz.GetAuthInfo(cpu);
            if (authenticationInfos.Count > 0 && authenticationInfos[0].e > DateTime.Now && authenticationInfos[0].c > 0)
            {
                bool isNetworkAvailable = NetworkInterface.GetIsNetworkAvailable();
                if (isNetworkAvailable)
                {
                    #region 签名验证
                    string str = "{\"physicalAddress\":\"" + cpu + "\"}";
                    string v = RSAHelper.EncryptByPublicKey(str);
                    AuthCommonResult result = InterfaceBiz.GetAuth(v);
                    if (!result.success)
                    {
                        MessageDialog.ShowDialog(result.message);
                        TextLogUtil.Info(result.message);
                        return;
                    }

                    string infoRSA = RSAHelper.DecryptByPublicKey(result.data.encryptContent);
                    AuthenticationInfo authenticationInfo = new JavaScriptSerializer().Deserialize<AuthenticationInfo>(infoRSA);
                    if (authenticationInfo == null)
                    {
                        MessageDialog.ShowDialog("未查询到授权信息，请联系管理员");
                        return;
                    }
                    string sign = RSAHelper.DecryptByPublicKey(result.data.signature);
                    string json = "{\"p\":\"" + authenticationInfos[0].p + "\",\"s\":\"" + authenticationInfos[0].s + "\",\"c\":" + authenticationInfos[0].c + ",\"t\":" + authenticationInfos[0].t + ",\"e\":\"" + authenticationInfos[0].e.ToString("yyyy-MM-dd HH:mm:ss") + "\"}";
                    string signLocal = RSAHelper.SHA256(json);
                    if (sign != signLocal)
                    {
                        MessageDialog.ShowDialog("签名异常，请联系管理员");
                        Environment.Exit(0);
                        return;
                    }

                    #endregion
                }

                Client client = new Client();
                this.Hide();
                client.Show();
            }
            else
            {
                AuthExpiresView authExpiresView = new AuthExpiresView();
                this.Visibility = Visibility.Hidden;
                if (authExpiresView.ShowDialog() == true)
                {
                    Client client = new Client();
                    this.Hide();
                    client.Show();
                }
            }
        }
    }
}
